<?php

$host = 'localhost';
$username = 'root';
$password = 123456;

$conn = mysql_connect($host,$username,$password);

//选择一个数据库
mysql_select_db("test");
$table_name="wendu3";
 $mysql_command="select wendu  from ".$table_name;
   $result=mysql_query($mysql_command);/*or die ("数据表：$table_name 查找记录失败！".mysql_error());
  echo "成功查找数据表：".$table_name."的记录。"; 
   echo "<br>"; */
   $data1= array();
   $data= array();
   /*for($i=0;$i<$result;$i++){
echo "" . mysql_result($mysql_command,$i) ."";

}*/
   while( $record=mysql_fetch_row($result))
   {
   
   $data1[] = $record[0];//把wendu存放到数组里
  
}
  //$data=implode(",",$data);
 for($i=0;$i<count($data1);$i++)
{

	$data[$i] = intval($data1[$i]);

}

 //print_r( $data);
header("Content-Type:image/png");

    $width = 225;			//定义画布宽度
    $height= 120;   		        //定义画布高度
    $right = 20;			//定义坐标系距画布右侧距离
    $left  = 20;			//定义坐标系距画布左侧距离
    $top   = 20;			//定义坐标系距画布上侧距离
    $buttom= 20;			//定义坐标系距画布下侧距离
    $up=33;                             //上基准线的值
    $down=23;                           //下基准线的值
    $target=30;                         //标准线的值



    $x = array();			//定义x坐标空数组
    $y = array();			//定义y坐标空数组
	


            //定义所绘制数据数组


 $name = array('a','b','c','d','e','f','b','c','d','e');		//定义横坐标的坐标标签

    foreach($data as $k=>$v){                                     //获取$data中最大值
   if( $k== 0  && $v !='' ){
      $min = $v;
      $max = $v;
   }elseif( $v !='' )  {
      $min = min($min,$v);
      $max = max($max,$v);
   }
   if($v == ''){
   $kong = $k;
   }
}
    
    $image = imagecreatetruecolor($width, $height);       //---------------------//
    $white = imagecolorallocate($image, 255, 255, 255);    //定义真彩画布及颜色值//
    $black = imagecolorallocate($image, 10, 10, 10);
    $blue  = imagecolorallocate($image, 0, 0, 255);
    $red  =	 imagecolorallocate($image, 255, 0, 0); 	//---------------------//
    $yellow  =	 imagecolorallocate($image, 255, 170, 0);
    $green=  imagecolorallocate($image, 79, 144, 30);
    imagefill($image, 0, 0, $white);					    //用白色填充画布

     //画横坐标
    imageline($image, $left,$height-$buttom, $width-$right, $height-$buttom, $black);


      //画纵坐标
    imageline($image, $left, $top , $left, $height-$buttom, $black);

      //画箭头
    imageline($image,$width-$right, $height-$buttom, ($width-$right)-8,$height-$buttom-3,  $black);
    imageline($image,$width-$right, $height-$buttom, ($width-$right)-8,$height-$buttom+3,  $black);

    imageline($image, $left-3, $top+5, $left, $top, $black);
    imageline($image, $left, $top, $left+3, $top+5, $black);

  //计算$data数组中每个元素的坐标
       $unit  =($width-$left-$right)/count($data);  //定义x轴刻度间隔
    for ($i = 0;$i < count($data);$i++){
    	$x[$i] = $left+$i*$unit;

		$y[$i] = $top+round($height-$top-$buttom)*(1-$data[$i]/$max);
    }

  //画出横坐标
    for ($i = 0;$i<count($data);$i++){

    	imageline($image, $left+$i*$unit, $height-$buttom, $left+$i*$unit, $height-$buttom-8, $black);
    	imagestring($image, 10, $left+$i*$unit, $height-$buttom, $name[$i], $black);
    }
   //画出纵坐标



    for($i = 0;$i<count($data);$i++){
    	imageline($image, $left, $top+($height-$top-$buttom)*$i/count($data), $left+5, $top+($height-$top-$buttom)*$i/count($data), $black);
    	imagestring($image, 10, $left/4, $top+($height-$top-$buttom)*$i/count($data), round($max*(count($data)-$i)/((count($data)))), $black);
    }

   //绘制数据折线图

 for ($i=0;$i<count($data);$i++){
    	if ($i+1!=count($data)){
        imageline($image, $x[$i], $y[$i], $x[$i+1], $y[$i+1], $blue);  //画线条
 		if ($data[$i]>$up||$data[$i]<$down){

    	imagefilledarc($image, $x[$i], $y[$i], 5, 5, 0, 360, $red, IMG_ARC_PIE);//画点
    }
    else{
    	imagefilledarc($image, $x[$i], $y[$i], 5, 5, 0, 360, $blue, IMG_ARC_PIE);//画点

    }

    	}
if ($data[$i]>$up||$data[$i]<$down){

    	imagefilledarc($image, $x[count($data)-1],$y[count($data)-1], 5, 5, 0, 360, $red,IMG_ARC_PIE);//画点
    }
    else{
    	imagefilledarc($image, $x[count($data)-1],$y[count($data)-1], 5, 5, 0, 360, $blue,IMG_ARC_PIE);//画点

    }
 }



    	//绘制折点标签值
    	 for ($i=0;$i<count($data);$i++){
    		if ($data[$i]>$up||$data[$i]<$down){

    	imagestring($image, 10, $x[$i]+5, $y[$i]+5, $data[$i], $red);
    }
    else{
    	imagestring($image, 10, $x[$i]+5, $y[$i]+5, $data[$i], $blue);

    }
}
/*画上基准线的值


    $upl=$top+round($height-$top-$buttom)*(1-$up/$max);
    imageline($image, $left,$upl, $width-$right, $upl, $yellow );
    imagestring($image, 5, $width-$right-57,$upl+2, "UCL="."$up", $yellow );

//画下基准线的值

     $downl=$top+round($height-$top-$buttom)*(1-$down/$max);
     imageline($image, $left,$downl, $width-$right, $downl,$yellow );
     imagestring($image, 5, $width-$right-57,$downl+2, "LCL="."$down", $yellow );

//画标准线的值

     $targetl=$top+round($height-$top-$buttom)*(1-$target/$max);
     imageline($image, $left,$targetl, $width-$right, $targetl, $green );
     imagestring($image, 5, $width-$right-57,$targetl+2, "tar="."$target",  $green);

*/
     //输出图形
    imagepng($image);

   //销毁图形资源
    imagedestroy($image);
	
?>